package controllers

import (
	"github.com/astaxie/beego/logs"
	"wjp/models"
	"wjp/syserror"
)

type IndexController struct {
	BaseController
}

// @router / [get]
func (this *IndexController) Index(){
	limit :=2
	//page
	page ,err :=this.GetInt("page",1)
	if err !=nil ||page <=0{
		page=1
	}
	title :=this.GetString("title")
	notes,err:=models.QueryNotesByPage(title,page,limit)
	if err!=nil{
		this.Abort500(err)
	}
	this.Data["notes"]=notes

	//这页数
	count,err :=models.QueryNotesCount(title)
	if err!=nil{
		this.Abort500(err)
	}

	totpage :=count/limit
	if count/limit !=0{
		totpage=totpage+1
	}
	this.Data["totpage"]=totpage
	this.Data["page"]=page
	this.Data["title"]=title
	this.TplName="index.html"
}

// @router /message [get]
func (this *IndexController) IndexMessage(){
	//this.Abort("404")
	this.TplName="message.html"
}

// @router /about [get]
func (this *IndexController) Indexabout(){
	this.TplName="about.html"
}

// @router /user [get]
func (this *IndexController) Indexuser(){
	this.TplName="user.html"
}
// @router /reg [get]
func (this *IndexController) Indexreg(){
	this.TplName="reg.html"
}

// @router /details/:key [get]
func (this *IndexController) IndexDetails(){
	key :=this.Ctx.Input.Param(":key")
	note,err :=models.QueryNoteByKey(key)
	if err !=nil{
		this.Abort500(syserror.New("文章不存在",err))
	}
	mss,err :=models.QueryMessageByNoteKey(key)
	if err !=nil{
		this.Abort500(syserror.New("文章不存在",err))
	}
	this.Data["message"]=mss
	logs.Info(mss)

	this.Data["note"]=note
	this.TplName="details.html"
}

// @router /comment/:key [get]
func (this *IndexController) IndexComment(){
	key :=this.Ctx.Input.Param(":key")
	note,err :=models.QueryNoteByKey(key)
	if err !=nil{
		this.Abort500(syserror.New("文章不存在",err))
	}
	this.Data["note"]=note
	this.TplName="comment.html"
}